package com.gree.scmservice.dao;

import com.gree.common.entity.SysApiRoleVo;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ApiRoleDao {

    @Autowired
    private Dao dao;

    public int getApiRoleCount(String comp, String filter){
        Sql sql = Sqls.create("select count(*) as nums from sys_api_role api " + filter);
        sql.setCallback(Sqls.callback.entity());
        sql.setEntity(dao.getEntity(Record.class));
        dao.execute(sql);
        Record countRecord=(Record) sql.getResult();
        int count= countRecord.getInt("nums");
        return count;
    }

    public List<Record> displayApiRole(String comp, String filter, int count, Pager pager){
        Sql sql = Sqls.create("select api.* from sys_api_role api " + filter);
        pager.setRecordCount(count);
        sql.setPager(pager);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(Record.class));
        List<Record> dataList=dao.execute(sql).getList(Record.class);
        return dataList;
    }

    public List<SysApiRoleVo> insertApiRole(List<SysApiRoleVo> sysApiRoleVos){
        List<SysApiRoleVo> sysApiRoleVoList = dao.insert(sysApiRoleVos);
        return sysApiRoleVoList;
    }

    public int updateApiRole(List<SysApiRoleVo> sysApiRoleVos){
       int num = dao.update(sysApiRoleVos);
       return num;
    }

    public int deleteApiRoleByIds(Long[] infoIds){
        int result = dao.clear("sys_api_role", Cnd.where("id","in",infoIds));
        return result;
    }
}
